#include <QCoreApplication>
#include"sqlcipher/sqlite3.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ERROR(X)

static int callback(void *NotUsed, int argc, char **argv, char **col_name)
{
    int i;
    for(i=0; i<argc; i++)
    {
        printf("打印结果;\n");
        printf("%s\n", argv[i] ? argv[i] : "NULL");
    }
    printf("\n");
    return 0;
}


int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    sqlite3 *db;
    const char *file= "test.db";
    const char *key = "123456";//key为使用sqlcipher设置的密码
    if (sqlite3_open(file, &db) == SQLITE_OK)
    {
        int  rc;

        if(db == NULL)
        {
            ERROR(("sqlite3_open reported OK, but db is null, retrying open %s\n", sqlite3_errmsg(db)))
        }
        //验证密码是否正确
        if(sqlite3_key(db, key, strlen(key)) != SQLITE_OK)
        {
            ERROR(("error setting key %s\n", sqlite3_errmsg(db)))
            exit(-1);
        }
        //SQLlite 操作代码...
        char* sql1 = "create table if not exists test(int id,varchar name);";
        char* sql2 = "insert into test values(1,'hello');";
        char* sql3 = "select * from test;";
        char* err_msg = NULL;
        rc = sqlite3_exec(db, sql1, callback, 0, &err_msg);
        if( rc!=SQLITE_OK )
        {

            fprintf(stderr, "SQL error: %s\n", err_msg);
            sqlite3_free(err_msg);
            err_msg = NULL;
        }
        rc = sqlite3_exec(db, sql2, callback, 0, &err_msg);
        if( rc!=SQLITE_OK )
        {
            fprintf(stderr, "SQL error: %s\n", err_msg);
            sqlite3_free(err_msg);
            err_msg = NULL;
        }

        rc = sqlite3_exec(db, sql3, callback, 0, &err_msg);
        if( rc!=SQLITE_OK )
        {
            fprintf(stderr, "SQL error: %s\n", err_msg);
            sqlite3_free(err_msg);
            err_msg = NULL;
        }
        sqlite3_close(db);

    }
    return a.exec();
}
